package com.meitianhui.convenitenbuy.module.store.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.store.contract.PreferentialDetailContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.CouponCardInfo;
import com.meitianhui.convenitenbuy.module.store.ui.activity.PreferentialDetailActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/29.
 */

public class PreferentialDetailPresenter implements PreferentialDetailContract.Presenter {

    private PreferentialDetailActivity mContext;
    private final PreferentialDetailContract.View mView;

    public PreferentialDetailPresenter(PreferentialDetailContract.View view,
                                       PreferentialDetailActivity context) {
        this.mView = view;
        this.mContext = context;
    }

    @Override
    public void getCouponDetail(String itemId) {
        Map<String, String> params = new HashMap<>();
        params.put("item_id", itemId);
        String paramJson = new Gson().toJson(params);

        StoreApiDataSource.getInstance()
                .getCouponCard(InfoProvider.getAppToken(App.getInstance()),
                        "goods.couponDetailForConsumerFind", paramJson)
                .subscribe(new ResponseObserver<BaseResponse<CouponCardInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<CouponCardInfo> response) {
                        if (response.data != null) {
                            if (response.data.detail != null) {
                                mView.getCouponDetailSuccess(response.data);
                            }
                        }
                    }
                });
    }

    @Override
    public void receiveCoupon(String itemId) {
        Map<String, String> param = new HashMap<>();
        param.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        param.put("member_type_key", "consumer");
        param.put("item_id", itemId);
        String paramJson = new Gson().toJson(param);

        StoreApiDataSource.getInstance()
                .receiveCoupon(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "goods.couponFree", paramJson)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.receiveCouponSuccess();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onFail(code, msg);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
